package co.edu.unal.bioingenium.kbmed.retrieval.main;

import co.edu.unal.bioingenium.kbmed.retrieval.data.RetrievalData;
import co.edu.unal.bioingenium.kbmed.retrieval.knowledge.process.RetrieveService;
import co.edu.unal.bioingenium.kbmed.retrieval.vo.RelevantDocument;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alejandro Riveros Cruz
 */
public class RetrieveCollectionMain {

    private RetrieveService retrieveService;

    /**
     * 
     * @param collectionName
     */
    public RetrieveCollectionMain(String collectionName) {
        retrieveService = RetrieveService.getInstance();
        retrieveService.init(collectionName);
    }

    /**
     * 
     */
    public void retrieveCycle() {
        long ini, fin;
        BufferedReader buffreader = new BufferedReader(new InputStreamReader(System.in));
        String query;
        List<RelevantDocument> wordResults;
        RetrievalData retrievalData = new RetrievalData();
        try {
            do {
                System.out.print("Insert query:");
                query = buffreader.readLine();
                ini = System.currentTimeMillis();
                retrieveService.retrieveByConcepts(retrievalData, query);
                wordResults = retrieveService.retrieveByWords(query);
                fin = System.currentTimeMillis();
                System.out.println("Retrieve by concepts");
                for (int i = 0; i < retrievalData.getResults().size(); i++) {
                    System.out.println((i + 1) + "--" + retrievalData.getResults().get(i));
                }
                System.out.println("Retrieve by words");
                for (int i = 0; i < wordResults.size(); i++) {
                    System.out.println((i + 1) + "--" + wordResults.get(i));
                }

                System.out.println("Total time: " + (double) (fin - ini) / 1000 + " seconds");
            } while (!query.equalsIgnoreCase("!q"));
        } catch (IOException ex) {
            Logger.getLogger(RetrieveCollectionMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * 
     * @param args
     */
    public static void main(String... args) {
        args = new String[1];
        args[0] = "saruro";
        if (args.length < 1) {
            System.out.println("Usage:");
            System.out.println("RetrieveCollectionMain corpusName");
            System.exit(1);
        } else {
            RetrieveCollectionMain retrieveCollectionMain = new RetrieveCollectionMain(args[0]);
            retrieveCollectionMain.retrieveCycle();
        }
    }
}
